<template>
  <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
    <el-form-item label="部门名称" prop="name">
      <el-input
        v-model="form.name"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input
        v-model="form.code"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width: 80%" placeholder="请选择">
        <el-option
          v-for="item in employees"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input
        v-model="form.introduce"
        style="width: 80%"
        placeholder="1-300个字符"
        type="textarea"
        :rows="3"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { getEmployeeSimple, updateDepartments } from '../../api/addDiaDep'
import { addDepartments } from '@/api/departments'
export default {
  name: '',
  props: {
    addDialogVisible: {
      type: Boolean
    },
    pid: {
      type: String
    },
    isEdit: {
      type: Boolean
    },
    ele: {
      type: Object,
      default:()=>{}
    }
  },
  data() {
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''
      },
      employees: [],
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' }
        ], // 部门名称
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
        ], // 部门编码
        manager: [
          { required: true, message: '请输入部门管理者', trigger: 'blur' }
        ], // 部门管理者
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          {
            min: 1,
            max: 300,
            message: '长度在 1 到 300 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {},
  watch: {
    isEdit: {
      handler(val) {
        if (val) {
          // this.form = this.ele
          // this.form = { ...this.ele }
          this.form = JSON.parse(JSON.stringify(this.ele))
        } else {
          this.form = {
            name: '', // 部门名称
            code: '', // 部门编码
            manager: '', // 部门管理者
            introduce: '', // 部门介绍
            pid: ''
          }
        }
      },
      immediate: true
    }
  },
  created() {
    this.loadEmployee()
  },
  methods: {
    hSubmit() {
      this.$refs.deptForm.validate(async(valid) => {
        if (!valid) return
        this.form.pid = this.pid
        const res = this.isEdit
          ? await updateDepartments(this.form)
          : await addDepartments(this.form)
        console.log(res, 123)
        if (res.data.code === 10000) {
          // this.$emit('update:addDialogVisible', false)
          // this.form = {
          //   name: '', // 部门名称
          //   code: '', // 部门编码
          //   manager: '', // 部门管理者
          //   introduce: '', // 部门介绍
          //   pid: ''
          // }
          this.$emit('loadAddDiaDep')
        }
      })
    },
    hCancel() {
      this.$emit('loadAddDiaDep')
    },
    async loadEmployee() {
      const res = await getEmployeeSimple()
      this.employees = res.data.data
    },
    // 清空表单
    hSuccess() {
      // this.$refs.deptForm.resetField()
      this.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''
      }
    }
  }
}
</script>
<style lang="less" scoped></style>
